// EWFCTRLDlg.cpp : implementation file
//
// Copyright (c) 2004 SJJ Embedded Micro Solutions, LLC
// All Rights Reserved
// Code is provided AS IS without any warranty

#include "stdafx.h"
#include "EWFCTRL.h"
#include "EWFCTRLDlg.h"
#include "ewfapi.h"

extern "C"
{
#include "xpepm.h"
#include ".\ewfctrldlg.h"
}

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CEWFCTRLDlg dialog



CEWFCTRLDlg::CEWFCTRLDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEWFCTRLDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEWFCTRLDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CEWFCTRLDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_Shutdown, OnBnClickedShutdown)
	ON_BN_CLICKED(IDC_Reboot, OnBnClickedReboot)
	ON_BN_CLICKED(IDC_Enable, OnBnClickedEnable)
	ON_BN_CLICKED(IDC_Disable, OnBnClickedDisable)
	ON_BN_CLICKED(IDC_COMMIT, OnBnClickedCommit)
END_MESSAGE_MAP()


// CEWFCTRLDlg message handlers

BOOL CEWFCTRLDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	UpdateEWFStatus();
    
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEWFCTRLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEWFCTRLDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEWFCTRLDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
void CEWFCTRLDlg::UpdateEWFStatus()
{
	DWORD dwStatus = ERROR_SUCCESS;
	BOOL bResult =FALSE;
	HANDLE hDisk = INVALID_HANDLE_VALUE;;
	PEWF_VOLUME_CONFIG pVolConfig = NULL;
	PEWF_VOLUME_NAME_ENTRY pProVolList = NULL;
	WCHAR szVolumeName[EWF_MAX_DEVICE_NAME_LENGTH + 15] = {0};
	WCHAR szDrive[20];
	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);

	pVolConfig = EwfMgrGetProtectedVolumeConfig(hDisk);

	switch (pVolConfig->Type)
		{
			case EWF_DISK:      SetDlgItemText(IDC_OTEDIT, "EWF_DISK"); break;
			case EWF_RAM:       SetDlgItemText(IDC_OTEDIT, "EWF_RAM");     break;
			case EWF_RAM_REG:   SetDlgItemText(IDC_OTEDIT, "EWF_RAM_REG"); break;
			default:
			MessageBox("ERROR: Unknown Ewf type", "Error", MB_OK);
			break;
		}

	switch (pVolConfig->State)
		{
			case EWF_ENABLED:   SetDlgItemText(IDC_STEDIT, "EWF_ENABLED");  break;
			case EWF_DISABLED:  SetDlgItemText(IDC_STEDIT, "EWF_DISABLED"); break;
			default:
			MessageBox("ERROR: Unknown Ewf state","Error", MB_OK);
			break;
		}
	
	switch (pVolConfig->BootCommand.Command)
		{
			case EWF_NO_CMD:    SetDlgItemText(IDC_CMDEDIT, "EWF_NO_CMD");  break;
			case EWF_ENABLE:    SetDlgItemText(IDC_CMDEDIT, "EWF_ENABLE");  break;
			case EWF_DISABLE:   SetDlgItemText(IDC_CMDEDIT, "EWF_DISABLE");  break;
			case EWF_COMMIT:    SetDlgItemText(IDC_CMDEDIT, "EWF_COMMIT");  break;
			case EWF_SET_LEVEL: SetDlgItemText(IDC_CMDEDIT, "EWF_SET_LEVEL");  break;
			default:
			MessageBox("ERROR: Unknown Ewf command", "Error", MB_OK);   
			break;
		}
	
	// Close Handle and Clear pVolConfig
	LocalFree(pVolConfig);
	pVolConfig = NULL;
	EwfMgrClose(hDisk);

}

void CEWFCTRLDlg::OnBnClickedShutdown()
{
	// TODO: Add your control notification handler code here
	XPE_Shutdown();

}

void CEWFCTRLDlg::OnBnClickedReboot()
{
	// TODO: Add your control notification handler code here
	XPE_ReStart();

}

void CEWFCTRLDlg::OnBnClickedEnable()
{
	// TODO: Add your control notification handler code here

	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);

	bResult = EwfMgrEnable(hDisk);
	if (!bResult)
	{
		MessageBox("EwfMgrEnable failed", "Error", MB_OK);
	}
	else
	{
		MessageBox("EWF Enabled -Reboot system for change to take effect", "EWF Enabled", MB_OK);
	}    
	
	EwfMgrClose(hDisk);

	UpdateEWFStatus();



}

void CEWFCTRLDlg::OnBnClickedDisable()
{
	// TODO: Add your control notification handler code here
	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);		
		
	bResult = EwfMgrDisable(hDisk,FALSE);
	if (!bResult)
	{
		MessageBox("EwfMgrDisable failed", "Error", MB_OK);
	}
	else
	{
		MessageBox("EWF Disabled - Reboot system for change to take effect", "EWF Disabled", MB_OK);
	}
    	
	EwfMgrClose(hDisk);
	UpdateEWFStatus();
}

void CEWFCTRLDlg::OnBnClickedCommit()
{
	// TODO: Add your control notification handler code here
	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);					
		
	bResult = EwfMgrCommit(hDisk);
	if (!bResult)
	{
		MessageBox("EwfMgrCommit failed", "Error", MB_OK);
	}
	else
	{
		MessageBox("Changes commited - Reboot system for change to take effect", "EWF Commit", MB_OK);
	}
    	
	EwfMgrClose(hDisk);
	UpdateEWFStatus();

}
